﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace CSBackup_Web.Sources.Override
{
    /// <summary>
    /// C#重写原C++的ToolBackupInfo类
    /// </summary>
	class ToolBackupInfo
	{
        //类域字符串常量，对应原C++类里的宏定义
        public static readonly string BACKUP_RECORD_ROOT_NODE = @"listInfo";
        public static readonly string BACKUP_RECORD_NODE_BACKUP_NAME = @"backupName";
        public static readonly string BACKUP_RECORD_NODE_BACKUP_TYPE = @"backupType";
        public static readonly string BACKUP_RECORD_NODE_BACKUP_TIME = @"backupTime";
        public static readonly string BACKUP_RECORD_NODE_BACKUP_FILE_AMOUNT = @"backupfileAmmount";
        public static readonly string BACKUP_RECORD_NODE_BACKUP_FILE_SIZE = @"backupfileSize";
        public static readonly string BACKUP_RECORD_NODE_DEPENDED_BACKUP = @"dependedBackup";
        public static readonly string BACKUP_RECORD_NODE_BACKUP_UNIT = @"backupUnit";
        public static readonly string BACKUP_RECORD_NODE_ENCRYPY = @"encrypt";
        public static readonly string BACKUP_RECORD_NODE_HIDE = @"hide";
        public static readonly string BACKUP_RECORD_NODE_AUTOSEP = @"autosep";

        protected static int AddBackupInfo(string path, string bkpName, List<BackupInfo> list)
        {
            if ((null == path) || (null == bkpName) || (null == list))
                return 1;
            string bkpCfgFile = string.Empty;
            if ('\\' == path[path.Length - 1])
                bkpCfgFile = path + BackupInfo.BACKUP_CONFIG_FILE_NAME;
            else
                bkpCfgFile = path + "\\" + BackupInfo.BACKUP_CONFIG_FILE_NAME;
            //如果备份配置文件不存在，则跳过此次备份，但告知调用者找不到文件
            if (!File.Exists(bkpCfgFile))
                return 2;

            BackupInfo bkpInfo = new BackupInfo();
            bkpInfo.BkpLocation = path;
            bkpInfo.BkpName = bkpName;

            //读取备份配置文件（XML文件）
            bool right = true;
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode node = null;

            try
            {
                xmlDoc.Load(bkpCfgFile);
                node = xmlDoc.SelectSingleNode(@"//" + BACKUP_RECORD_NODE_BACKUP_TYPE);
                if (node != null)
                    bkpInfo.BkpType = node.InnerText;
                else
                    right = false;
                node = xmlDoc.SelectSingleNode(@"//" + BACKUP_RECORD_NODE_BACKUP_TIME);
                if (node != null)
                    bkpInfo.BkpTime = node.InnerText;
                else
                    right = false;
                node = xmlDoc.SelectSingleNode(@"//" + BACKUP_RECORD_NODE_BACKUP_FILE_AMOUNT);
                if (node != null)
                    bkpInfo.BkpFileAmount = node.InnerText;
                else
                    right = false;
                node = xmlDoc.SelectSingleNode(@"//" + BACKUP_RECORD_NODE_BACKUP_FILE_SIZE);
                if (node != null)
                    bkpInfo.BkpFileSize = node.InnerText;
                else
                    right = false;
                node = xmlDoc.SelectSingleNode(@"//" + BACKUP_RECORD_NODE_DEPENDED_BACKUP);
                if (node != null)
                    bkpInfo.BkpDepended = node.InnerText;
                else
                    right = false;
                node = xmlDoc.SelectSingleNode(@"//" + BACKUP_RECORD_NODE_BACKUP_UNIT);
                if (node != null)
                    bkpInfo.BkpUnit = node.InnerText;
                else
                    right = false;
                node = xmlDoc.SelectSingleNode(@"//" + BACKUP_RECORD_NODE_AUTOSEP);
                if (node != null)
                    bkpInfo.SelfSep = (node.InnerText != @"false");
                else
                    right = false;
            }
            catch (Exception exp)
            {
                //在这里可以添加信息到日志面板
                Debug.WriteLine("Exp in AddBackupInfo(): " + exp.Message);
                return 3;
            }

            if (!right)
                return 3;

            list.Add(bkpInfo);
            return 0;
        }

        /// <summary>
        /// 从文件读取备份列表
        /// </summary>
        /// <param name="path">备份文件夹全路径</param>
        /// <param name="list">一个备份文件里的信息对应list中的一个节点</param>
        /// <returns>0 -- 成功 ； 1 -- 参数错误 ； 2 -- 找不到文件 ； 3 -- 文件内容有误</returns>
        protected static int LoadBkpInfoList(string path, List<BackupInfo> list)
        {
            if ((null == path) || (string.Empty == path))
                return 1;
            if (null == list)
                return 1;

            list.Clear();
            //四个备份文件夹是在工程创建之时就一起创建好的，不可能不存在
            if (!Directory.Exists(path))
                return 1;

            //每种备份方式的每一次备份，都会产生一个单独的文件夹，所以要遍历
            //备份文件夹下的所有子文件夹；如果没有子文件，说明没有执行过此种备份
            string dirName = string.Empty;
            string[] subDirs = Directory.GetDirectories(path);
            if (0 == subDirs.Length)
                return 0;

            foreach (string dir in subDirs)
            {
                dirName = dir.TrimEnd(new Char[] { '\\' });
                dirName = dirName.Substring(dirName.LastIndexOf('\\') + 1);
                AddBackupInfo(dir, dirName, list);
            }

            return 0;
        }

        public static int LoadFullBkpInfoList(string path, List<BackupInfo> list)
        {
            return LoadBkpInfoList(path, list);
        }

        public static int LoadIncBkpInfoList(string path, List<BackupInfo> list)
        {
            return LoadBkpInfoList(path, list);
        }

        public static int LoadDiffBkpInfoList(string path, List<BackupInfo> list)
        {
            return LoadBkpInfoList(path, list);
        }

        public static int LoadDiffIncBkpInfoList(string path, List<BackupInfo> list)
        {
            return LoadBkpInfoList(path, list);
        }
	}//end of class
}
